package net.thxjj.mobileServer.websocket;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import net.thxjj.mobileServer.entity.*;
import net.thxjj.mobileServer.util.ParameterMap;
import net.thxjj.mobileServer.util.Tools;

import java.util.HashMap;
import java.util.logging.Logger;

public class AppSendHandler extends SesstionBaseHandler{

    private Logger log = Logger.getLogger(String.valueOf(this.getClass()));

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame baseBean) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
    }

    @Override
    public void chanelReadR(ChannelHandlerContext ctx, TextWebSocketFrame s) {
        AppSendBean appSendBean =  JSONObject.parseObject(s.text(), AppSendBean.class);

        //判断appTid是否为空
        String appTid = appSendBean.getParams().getAppTid();
        if(Tools.isEmpty(appTid)){
            AppRespBean appRespBean =  new AppRespBean(appSendBean.getMsgId(), ActionType.ERROR_RESP.getName(), ErrCode.APPTID_EMPTY_ERROR.getCode(),ErrCode.APPTID_EMPTY_ERROR.getDescription());
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
            ctx.close();
            return;
        }

        AttributeKey<String> key = AttributeKey.valueOf("user");
        String access_token = ctx.channel().attr(key).get();

        //JWT验证失败，即不存在
        ParameterMap parameterMap = userService.getUserSnapshot(access_token);
        if(parameterMap==null){
            AppRespBean appRespBean =  new AppRespBean(appSendBean.getMsgId(), ActionType.ERROR_RESP.getName(), ErrCode.JWT_NOT_EXIST_ERROR.getCode(),ErrCode.JWT_NOT_EXIST_ERROR.getDescription());
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
            ctx.close();
            return;
        }

        String app_tid = parameterMap.getString("app_tid");
        if(!appTid.contains(app_tid)){
            AppRespBean appRespBean =  new AppRespBean(appSendBean.getMsgId(), ActionType.ERROR_RESP.getName(), ErrCode.APPTID_CHECK_ERROR.getCode(),ErrCode.APPTID_CHECK_ERROR.getDescription());
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
            ctx.close();
            return;

        }
        long user_id = Long.parseLong(parameterMap.getString("user_id"));
        //数据有效性校验（包括设备是否上线，cmd是否有效，数据范围是否超出）

        long code = 200;
        String msg = "123";

        if(code==200){
            AppRespBean appRespBean =  new AppRespBean(appSendBean.getMsgId(), ActionType.APP_SENDRESP.getName(), ErrCode.SUCCESS.getCode(),ErrCode.SUCCESS.getDescription());
            AppSendRespParams appSendParams = new AppSendRespParams();
            appSendParams.setAppTid(appSendBean.getParams().getAppTid());
            appSendParams.setCtrlKey(appSendBean.getParams().getCtrlKey());
            appSendParams.setDevTid(appSendBean.getParams().getDevTid());
            appSendParams.setData(appSendBean.getParams().getData());
            appRespBean.setDevRespParams(appSendParams);
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
        } else {
            AppRespBean appRespBean =  new AppRespBean(appSendBean.getMsgId(), ActionType.ERROR_RESP.getName(), code, msg);
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
        }


    }

}
